package com.hxd.otc.module.my.model;

import com.hxd.otc.api.IBankApi;
import com.hxd.otc.base.BaseBean;
import com.hxd.otc.common.enums.BankType;
import com.hxd.otc.module.my.bean.BankCardBean;
import com.hxd.otc.module.my.bean.CurrencyBean;
import com.hxd.otc.net.BaseObserver;
import com.hxd.otc.net.RetrofitManager;
import com.uber.autodispose.AutoDisposeConverter;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;

import static com.hxd.otc.common.enums.BankType.ALIPAY;
import static com.hxd.otc.common.enums.BankType.BANK;
import static com.hxd.otc.common.enums.BankType.WEXIN;

/**
 * Created by lzy on 2018/6/14 0014.
 * description ${TODO}
 * <p>
 * Changed by somebody on 2018/6/14 0014
 * description ${TODO}
 */
public class BankModel {

    private static class SingletonHolder {
        private static final BankModel instance = new BankModel();
    }

    public static BankModel getInstance() {
        return BankModel.SingletonHolder.instance;
    }

    /**
     * 添加收款方式
     * @param id    当创建时不传
     * @param type  收款类型
     * @param payNo 支付号码
     * @param qrcodeId  二维码图片id
     * @param bankName  银行卡名
     * @param branchBankName    支行名称
     * @param country   国家
     * @param status    0 弃用，1使用中
     * @param mobile    手机
     * @param realName  名字
     * @param cardNo    身份证
     * @param supportCurrencyCode   //支持币种，eg:  CNY,USD
     */
    public void bindTradeMode(String id,BankType type,String payNo,String qrcodeId,String bankName,String branchBankName,String country,
            int status,String mobile,String realName,String cardNo,String supportCurrencyCode, BaseObserver observer, AutoDisposeConverter<BaseBean> converter) {

        getBankApi().bindTradeMode(id,type.getType(),payNo,qrcodeId,bankName,branchBankName,country,status,mobile,realName,cardNo,supportCurrencyCode)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }

    public void addBank(String payNo,String bankName,String branchBankName,String country, String realName,
                        String supportCurrencyCode, BaseObserver observer, AutoDisposeConverter<BaseBean> converter) {
        bindTradeMode(null,BankType.BANK,payNo,null,bankName,branchBankName,country,1,"",realName,"",supportCurrencyCode,observer,converter);
    }

    public void updateBank(String id,String payNo,String bankName,String branchBankName,String country, int status,
                        String realName,String supportCurrencyCode, BaseObserver observer, AutoDisposeConverter<BaseBean> converter) {
        bindTradeMode(id,BankType.BANK,payNo,null,bankName,branchBankName,country,status,"",realName,"",supportCurrencyCode,observer,converter);
    }


    public void addAlipayOrWinXin(BankType bankType,String payNo,String qrcodeId,String country, String realName,
                          BaseObserver observer, AutoDisposeConverter<BaseBean> converter) {
        bindTradeMode(null,bankType,payNo,qrcodeId, bankType.getBankName(),"",country,1,"",realName,"","CNY",observer,converter);
    }

    public void updateAlipayOrWinXin(BankType bankType,String id,String payNo,String qrcodeId,String country, int status,
                          String realName, BaseObserver observer, AutoDisposeConverter<BaseBean> converter) {
        bindTradeMode(id,bankType,payNo,qrcodeId, bankType.getBankName(),"",country,status,"",realName,"","CNY",observer,converter);
    }

    public void getCurrencyList(BaseObserver<ArrayList<CurrencyBean>> observer,AutoDisposeConverter<BaseBean<ArrayList<CurrencyBean>>> converter){
        getBankApi().getCurrencyList()
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }

    public void getBankCardList(BaseObserver<ArrayList<BankCardBean>> observer, AutoDisposeConverter<BaseBean<ArrayList<BankCardBean>>> converter) {
        getBankApi().getBankCardList(1)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }

    public void getBankCardEasyList(BaseObserver<ArrayList<BankCardBean>> observer, AutoDisposeConverter<BaseBean<ArrayList<BankCardBean>>> converter) {
        getBankApi().getBankCardEasyList(1)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }

    /**
     * 通过订单id获取用户收款方式，主要用于交易卖出时获取用户的收款方式
     *
     * @param orderId 交易条目的订单id
     */
    public void getUserBankCardByOrderId(String orderId, BaseObserver<ArrayList<BankCardBean>> observer, AutoDisposeConverter<BaseBean<ArrayList<BankCardBean>>> converter) {
        getBankApi().getUserBankCardByOrderId(orderId)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }

    public List<BankType> getTradingModeList() {
        List<BankType> list = new ArrayList<>();
        list.add(BANK);
        list.add(ALIPAY);
        list.add(WEXIN);
        return list;
    }

    private IBankApi getBankApi() {
        return RetrofitManager.getDefault().create(IBankApi.class);
    }
}
